<?php
//decode by http://奥森世纪/
namespace app\home\controller;

use app\home\model\CreditRecord;
use app\home\model\Member;
use think\Db;
use think\Log;
use think\Request;

use app\admin\model\Config;

class Connection extends Base{
	
	
    public function detail(Request $request){
    	$connection_id=$request->param('connection_id');
    	$item=db('connection')->find($connection_id);
    	
    	db('connection')->where('connection_id',$connection_id)->setInc('click');
    	
    	$next_where=[
    			'connection_id'=>['gt',$connection_id],
    			'checked'=>1,
    		];
    	$next_item=db('connection')->where($next_where)->order('connection_id asc')->find();
    	
    	$last_where=[
    			'connection_id'=>['lt',$connection_id],
    			'checked'=>1,
    		];
    		
    	$last_item=db('connection')->where($last_where)->order('connection_id desc')->find();
    	
    	//分类
    	
    	if($item){
    		if($item['type'] == 1){
    			$item['type_name']='个人微信';
    		}
    		if($item['type'] == 2){
    			$item['type_name']='微信群';
    		}
    		if($item['type'] == 3){
    			$item['type_name']='小程序';
    		}
    		if($item['type'] == 4){
    			$item['type_name']='公众号';
    		}
    		if($item['type'] == 5){
    			$item['type_name']='微商货源';
    		}
    		if($item['type'] == 6){
    			$item['type_name']='QQ群';
    		}
    		
    	}
    	
    	$this->assign('last_item',$last_item);
    	$this->assign('next_item',$next_item);
    	$this->assign('item',$item);
    	return $this->fetch(__FUNCTION__);
    	
    }
    public function index(Request $request){
    		$keyword=$request->param('keyword');
    		$type=$request->param('type');
    		
		        //分页
		    $size=params('size')?:20;
		    $page=params('page')?:1;
		
		    $start=$size*($page-1);
    	
    		$param=[];
            $keyword = trim(params('keyword'));
	        if($keyword != ''){
	          if(is_numeric($keyword)){
	            $param['connection_id']=$keyword;
	          }else{
	            $param['title']=['LIKE','%'.$keyword.'%'];
	          }
	
	        }
	
	         $type = $request->param('type');
    		if($type){
	            $param['type']=$type;
	          }
	          
	          $param['checked']=1;
	          
    		$tasks=db('connection')->where($param)->order('top_time desc,refresh_time desc')->limit($start,$size)->select();
    		

    		
    		if(request()->isAjax()){
              return  $this->fetch('_index',['tasks'=>$tasks]);
            }
            
    		
    		
    		
    		$this->assign('tasks',$tasks);
    		$this->assign('type',$type);
    		$this->assign('keyword',$keyword);
        return $this->fetch(__FUNCTION__);
    }
    
	
    public function select(){
        return $this->fetch(__FUNCTION__);
    }
    
    
    
    public function publish(Request $request){
    	  $member = $this->checkLogin();
    	  
    	  $data=[];
    	  $id=$request->param('id');
    	  
    	  $type=$request->param('type');
    	  
    	  if($id){
    	  	$data=db('connection')->find($id);
    	  	$type=$data['type'];
    	  }
    	  
    	  
			//余额是否够
			$config = Config::getInfo();
            $fee = 0;
            if(!empty($config['setting']['rm_fee_'.$type])){
                $fee = floatval(trim($config['setting']['rm_fee_'.$type]));
            }
    	  
    	  
    	  
    	  if(request()->isAjax()){
    	  	
    	  	$title=$request->param('title');
    	  	$introduce=$request->param('introduce');
    		$type=$request->param('type');
    		if(!$type){
                message('请选择类型','','error');
            }
    	  	if(!$title){
                message('请输入标题','','error');
            }
            
            if(!$introduce){
                message('请输入介绍','','error');
            }


            
           //余额是否充足
            if($member['credit2']<$fee){
            	 message('余额不足，请充值','','error');
            }
            $photo=$request->file('photo');
            $qrcode=$request->file('qrcode');
            
            if(!$photo && !$data){
                message('请上传头像','','error');
            }
            if(!$qrcode && !$data){
                message('请上传二维码','','error');
            }
            
            $photo_url=$qrcode_url='';
            
            if($photo){
  	        	 $photo_info = $this->qiniuupload($photo);
  	        	 
	            if(!$photo_info){
	                message('图片上传失败','','error');
	            }
	            
	        	 $photo_url = 'http://'.$photo_info;
            }
            
            if($qrcode){

	  	         $qrcode_info = $this->qiniuupload($qrcode);
	            if(!$qrcode_info){
	                message('图片上传失败','','error');
	            }
	  	         
	            $qrcode_url = 'http://'.$qrcode_info;
            }
	            

	            
	            
	            
	            $insert_data=[
	            	'title'=>$title,
	            	'introduce'=>$introduce,
	            	'uid'=>$member['uid'],
	            ];
	            
	  
             Db::startTrans();
             if($id){
             	$insert_data['photo']=$photo_url?:$data['photo'];
             	$insert_data['qrcode']=$qrcode_url?:$data['qrcode'];
             	$insert_data['connection_id']=$id;
             	
	            $rst=db('connection')->update($insert_data);
	            if($rst){
		            Db::commit();
		            message('修改成功','reload','success');
	            }else{
	            	 Db::rollback();
	                message('修改失败','','error');
	            }
	            
             }else{
             	$insert_data['type']=$type;
             	$insert_data['photo']=$photo_url;
             	$insert_data['qrcode']=$qrcode_url;
             	
             	$insert_data['add_time']=TIMESTAMP;
             	
	            $rst=db('connection')->insert($insert_data);
	            if($rst){
	            	//扣除费用
	            	 //记录提现操作
	            	 $result=db('member')->where('uid',$member['uid'])->setDec('credit2',$fee);
	            	 if($result){
	            	 	
			            $status2 = CreditRecord::addInfo([
			                'uid' => $member['uid'],
			                'type' => 'credit2',
			                'num' => -$fee,
			                'title' => '发布人脉',
			                'remark' => "发布人脉信息，扣除{$fee}余额。",
			                'create_time' => TIMESTAMP
			            ]);
			            if(!$status2){
			                Db::rollback();
			                message('发布失败：-2','','error');
			            }
	            	 }else{
	            	 		Db::rollback();
			                message('扣款失败：-2','','error');
	            	 }
	            	
	            	
	            	
		            Db::commit();
		            message('添加成功,请等待审核','reload','success');
	            }else{
	            	 Db::rollback();
	                message('添加失败','','error');
	            }
             }

            
    	  }
	
	
    	  $this->assign('fee',$fee);
    	  $this->assign('data',$data);
    	  $this->assign('type',$type);
        return $this->fetch(__FUNCTION__);
    }
    
       public function manage(Request $request){
       		$member=$this->checkLogin();
    		$keyword=$request->param('keyword');
    		$type=$request->param('type');
    		
		        //分页
		    $size=params('size')?:20;
		    $page=params('page')?:1;
		
		    $start=$size*($page-1);
    	
    		$param=[];
            $keyword = trim(params('keyword'));
	        if($keyword != ''){
	          if(is_numeric($keyword)){
	            $param['connection_id']=$keyword;
	          }else{
	            $param['title']=['LIKE','%'.$keyword.'%'];
	          }
	
	        }
	
	         $type = $request->param('type');
    		if($type){
	            $param['type']=$type;
	          }
	          
	       //   $param['checked']=1;
	          $param['uid']=$member['uid'];
    		//$tasks=db('connection')->where($param)->limit($start,$size)->select();
    		$tasks=db('connection')->where($param)->limit($start,$size)->select();
    		
    		$now=TIMESTAMP;
    		$model=db('connection');
			foreach($tasks as &$v){
				$v['expire_day']=floor(($v['add_time'] + 60*24*60*60-$now)/(60*60*24));
				$v['add_time']=date('Y-m-d H:i:s',$v['add_time']);
				//排名
				$rank=$model->where(['checked'=>1,'click'=>['gt',$v['click']]])->count();
				$v['rank']=$rank+1;
			}
			
    		
    		if(request()->isAjax()){
              return  $this->fetch('_index',['tasks'=>$tasks]);
            }
            

			//刷新费用
			$config = Config::getInfo();
            $refresh_fee = 0;
            if(!empty($config['setting']['rm_refresh'])){
                $refresh_fee = floatval(trim($config['setting']['rm_refresh']));
            }
            
    		$this->assign('refresh_fee',$refresh_fee);
    		$this->assign('tasks',$tasks);
    		$this->assign('type',$type);
    		$this->assign('keyword',$keyword);
        return $this->fetch(__FUNCTION__);
    }
       public function delete(Request $request){
	        if(request()->isAjax()){
	        	$member=$this->checkLogin();
            $id = $request->param('id');
            if(!$id){
                message('参数错误','','error');
            }
        	$status=db('connection')->where(['connection_id'=>$id,'uid'=>$member['uid']])->delete();
            if(!$status){
                message('删除失败','','error');
            }
            message("删除成功",'reload','success');
        }
       }
       
       public function top(Request $request){
       	 $id = $request->param('id');
       	 $member=$this->checkLogin();
       	 if(request()->isAjax()){
       		$type = $request->param('type');
       		
       		$fee=0;
       		if($type == 180){
       			$fee=900;
       		}
       		if($type == 90){
       			$fee=500;
       		}
       		if($type == 30){
       			$fee=200;
       		}
       		if($type == 15){
       			$fee=110;
       		}
       		if($type == 7){
       			$fee=60;
       		}
       		if($type == 3){
       			$fee=30;
       		}
       		if(!$fee || !$id){
       			message('缺少参数','','error');
       		}
	            $info=db('connection')->find($id);
       		
       		if($member['credit2'] < $fee){
       			message('余额不足','','error');
       		}
       		
       		if(!$info['checked']){
       			message('请等待审核','','error');
       		}
       		
       		if($info['checked'] == 2){
       			message('该信息审核未通过','','error');
       		}
       		
       		Db::startTrans();
       		
        	//扣除费用
        	 //记录提现操作
        	 $result=db('member')->where('uid',$member['uid'])->setDec('credit2',$fee);
        	 if($result){
        	 	
	            $status2 = CreditRecord::addInfo([
	                'uid' => $member['uid'],
	                'type' => 'credit2',
	                'num' => -$fee,
	                'title' => '置顶',
	                'remark' => "置顶id为{$id}的信息{$type}天，扣除{$fee}余额。",
	                'create_time' => TIMESTAMP
	            ]);
	            if(!$status2){
	                Db::rollback();
	                message('置顶失败：-1','','error');
	            }
	            
	            //更新时间
	            if($info['top_time'] < TIMESTAMP){
	            	$top_time=TIMESTAMP+$type*24*60*60;
	            }else{
	            	$top_time=$info['top_time']+$type*24*60*60;
	            }
	           $update_data['top_time']=$top_time;
	           $update_data['is_top']=1;
	            
	            $up_rst=db('connection')->where('connection_id',$id)->update($update_data);
	            
	            if($up_rst){
	            	
		            Db::commit();
		            message('置顶成功','reload','success');
	            }else{
        	 		Db::rollback();
	                message('置顶失败：-2','','error');
	            }
	            
	            
        	 }else{
        	 		Db::rollback();
	                message('扣款失败：-3','','error');
        	 }
        
       	 }
       	 
       	 $this->assign('id',$id);
        return $this->fetch(__FUNCTION__);
       	
       }
       
       public function refresh(Request $request){
       	 $id = $request->param('id');
       	 $member=$this->checkLogin();
       	 if(request()->isAjax()){
       		
			//余额是否够
			$config = Config::getInfo();
            $fee = 0;
            if(!empty($config['setting']['rm_refresh'])){
                $fee = floatval(trim($config['setting']['rm_refresh']));
            }
       		if(!$fee || !$id){
       			message('缺少参数','','error');
       		}
       		
       		$info=db('connection')->find($id);
       		
       		if($member['credit2'] < $fee){
       			message('余额不足','','error');
       		}
       		//用户是否置顶
       		if(!$info['is_top']){
       				message('请先置顶','','error');
       		}
       		Db::startTrans();
       		
        	//扣除费用
        	 //记录提现操作
        	 $result=db('member')->where('uid',$member['uid'])->setDec('credit2',$fee);
        	 if($result){
        	 	
	            $status2 = CreditRecord::addInfo([
	                'uid' => $member['uid'],
	                'type' => 'credit2',
	                'num' => -$fee,
	                'title' => '刷新',
	                'remark' => "刷新id为{$id}的信息，扣除{$fee}余额。",
	                'create_time' => TIMESTAMP
	            ]);
	            if(!$status2){
	                Db::rollback();
	                message('刷新失败：-1','','error');
	            }
	            
	            //更新时间
	       
	           $update_data['refresh_time']=TIMESTAMP;
	           $update_data['is_refresh']=1;
	            
	            $up_rst=db('connection')->where('connection_id',$id)->update($update_data);
	            
	            if($up_rst){
		            Db::commit();
		            message('刷新成功','reload','success');
	            }else{
        	 		Db::rollback();
	                message('刷新失败：-2','','error');
	            }
	            
        	 }else{
        	 		Db::rollback();
	                message('扣款失败：-3','','error');
        	 }
        
       	 }
       	 
       	
       }
	
	
       public function autoRefresh(Request $request){
       	 $id = $request->param('id');
       	 $member=$this->checkLogin();
       	 if(request()->isAjax()){
       		$type = $request->param('type');
       		
       		$fee=0;
       		$auto_time=0;
       		if($type == 1){
       			$fee=900;
       			$auto_time=1;
       		}
       		if($type == 2){
       			$fee=500;
       			$auto_time=3;
       		}
       		if($type == 3){
       			$fee=200;
       			$auto_time=10;
       		}
       		if($type == 4){
       			$fee=110;
       			$auto_time=30;
       		}
       		if($type == 5){
       			$fee=60;
       			$auto_time=60;
       		}
       		if($type == 6){
       			$fee=30;
       			$auto_time=120;
       		}
       		if(!$fee || !$id || !$auto_time){
       			message('缺少参数','','error');
       		}

            $info=db('connection')->find($id);
       		
       		if($member['credit2'] < $fee){
       			message('余额不足','','error');
       		}
       		

       		//用户是否置顶
       		if(!$info['is_top']){
       				message('请先置顶','','error');
       		}
       		
       		Db::startTrans();
       		
        	//扣除费用
        	 //记录提现操作
        	 $result=db('member')->where('uid',$member['uid'])->setDec('credit2',$fee);
        	 if($result){
        	 	
	            $status2 = CreditRecord::addInfo([
	                'uid' => $member['uid'],
	                'type' => 'credit2',
	                'num' => -$fee,
	                'title' => '自动刷新',
	                'remark' => "自动刷新id为{$id}的信息类型{$type}，扣除{$fee}余额。",
	                'create_time' => TIMESTAMP
	            ]);
	            if(!$status2){
	                Db::rollback();
	                message('刷新失败：-1','','error');
	            }
	            
	            //更新时间
	           
	           $update_data['auto_time']=$auto_time;
	           $update_data['is_auto']=1;
	           $update_data['refresh_time']=TIMESTAMP;
	            
	            $up_rst=db('connection')->where('connection_id',$id)->update($update_data);
	            
	            if($up_rst){
	            	
		            Db::commit();
		            message('自动刷新成功','reload','success');
	            }else{
        	 		Db::rollback();
	                message('自动刷新失败：-2','','error');
	            }
	            
	            
        	 }else{
        	 		Db::rollback();
	                message('扣款失败：-3','','error');
        	 }
        
       	 }
       	 
       	 $this->assign('id',$id);
        return $this->fetch(__FUNCTION__);
       	
       }
       
       
       //定时任务
	
	public function crond_refresh(){
		$model=db('connection');
		$data=$model->where(['checked'=>1])->select();
		$now=TIMESTAMP;
		$minute=date('Y-m-d H:i',$now);

		foreach($data as $k=>$v){
			$new_data=[];
			$new_data['connection_id']=$v['connection_id'];
			//置顶的处理
			if($v['top_time']<$now && $v['is_top'] == 1){
				$new_data['top_time']=0;
				$new_data['is_top']=1;
				$v['auto_time']=0;
				$v['is_auto']=0;
			}
			
			//刷新的处理
			if($v['auto_time'] && $v['is_auto']){
				
				//当前时间的分钟数与以前的分钟数加上间隔时间
				$calc=$v['refresh_time']+$v['auto_time']*60;
				$calc_minute=date('Y-m-d H:i',$calc);
				if($minute == $calc_minute){
					$v['refresh_time']=TIMESTAMP;
				}
			}
			$model->update($new_data);
		}
		
	}
	
	
}